package com.hp.bon.sgw.service;

import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.hp.bon.security.config.BonPublicKey;
import com.hp.bon.security.config.KeyManager;
import com.hp.bon.security.config.SecurityConfigManager;
import com.hp.bon.security.config.SupperKey;

@Path("/")
public class SecurityRestService {

	@GET
	@Path("security/reloadconfig")
	@Produces(MediaType.APPLICATION_JSON)
	public String reloadConfigFile() {
		try {
			SecurityConfigManager.getInstance().loadConfig();
			KeyManager.getInstance().loadService2NeCategory();
			return "SUCCESS";
		} catch (Exception e) {
			return e.toString();
		}
	}

	@GET
	@Path("security/reloaddeskey")
	@Produces(MediaType.APPLICATION_JSON)
	public String reloadAll() {
		try {
			KeyManager.getInstance().loadDESKey();
			KeyManager.getInstance().loadRSAKey();
			return "SUCCESS";
		} catch (Exception e) {
			return e.toString();
		}
	}

	@GET
	@Path("security/genpubkey")
	@Produces(MediaType.APPLICATION_JSON)
	public String triggerGenPubkey() {
		try {
			SecurityConfigManager.getInstance().needChangePubkey = true;
			return "trigger gen public key success";
		} catch (Exception e) {
			return e.toString();
		}
	}

	@GET
	@Path("security/queryPubkey/{destNe}/{version}")
	@Produces(MediaType.APPLICATION_JSON)
	public String queryPubkey(@PathParam("destNe") String destNe, @PathParam("version") String version) {
		try {

			return "trigger query public key success";
		} catch (Exception e) {
			return e.toString();
		}
	}

  @GET
  @Path("security/findpubkey/{destNe}/{version}")
  @Produces({"application/json"})
  public String getPublicKey(@PathParam("destNe") String neCategoryAndDomain, @PathParam("version") String version)
  {
    BonPublicKey key = KeyManager.getInstance().getPublicKey(neCategoryAndDomain, version);
    return new Gson().toJson(key);
  }
	@GET
	@Path("security/pubkey")
	@Produces(MediaType.APPLICATION_JSON)
	public String getRsaPublicKey() {
		Map<String, ArrayList<SupperKey>> map = KeyManager.getInstance().getRsaPublicKeyMap();
		StringBuilder str = new StringBuilder();
		for (Entry<String, ArrayList<SupperKey>> entry : map.entrySet()) {
			str.append(entry.getKey() + "\r\n");
			for (SupperKey key : entry.getValue()) {
				str.append("  - " + key.getNeCategoryAndDomain() + "|" + key.getKeyVersion() + "|" + key.getEffectTime() + "|" + key.getRetryTimes() + "|" + key.getLastSendTime() + "\r\n");
			}
		}
		return str.toString();
	}

	@GET
	@Path("security/indeskey")
	@Produces(MediaType.APPLICATION_JSON)
	public String getInRequestDesKey() {
		Map<String, ArrayList<SupperKey>> map = KeyManager.getInstance().getInRequestDesKeyMap();
		StringBuilder str = new StringBuilder();
		for (Entry<String, ArrayList<SupperKey>> entry : map.entrySet()) {
			str.append("\r\n" + entry.getKey() + "\r\n");
			for (SupperKey key : entry.getValue()) {
				str.append("  - " + key.getNeCategoryAndDomain() + "|" + key.getKeyVersion() + "|" + key.getEffectTime() + "|" + key.getRetryTimes() + "|" + key.getLastSendTime() + "\r\n");
			}
		}
		return str.toString();
	}

	@GET
	@Path("security/outdeskey")
	@Produces(MediaType.APPLICATION_JSON)
	public String getOutRequestDesKey() {
		Map<String, ArrayList<SupperKey>> map = KeyManager.getInstance().getOutRequestDesKeyMap();
		StringBuilder str = new StringBuilder();
		for (Entry<String, ArrayList<SupperKey>> entry : map.entrySet()) {
			str.append("\r\n" + entry.getKey() + "\r\n");
			for (SupperKey key : entry.getValue()) {
				str.append("  - " + key.getNeCategoryAndDomain() + "|" + key.getKeyVersion() + "|" + key.getEffectTime() + "|" + key.getRetryTimes() + "|" + key.getLastSendTime() + "\r\n");
			}
		}
		return str.toString();
	}

	@GET
	@Path("security/privatekey")
	@Produces(MediaType.APPLICATION_JSON)
	public String getMyRsaPrivateKeyList() {
		ArrayList<SupperKey> list = KeyManager.getInstance().getMyRsaPrivateKeyList();
		StringBuilder str = new StringBuilder();
		for (SupperKey entry : list) {
			str.append(new Gson().toJson(entry) + "\r\n");
		}
		return str.toString();
	}

	@GET
	@Path("security/resendoutdeskey")
	@Produces(MediaType.APPLICATION_JSON)
	public String resendOutDesKey() {
		SecurityConfigManager.getInstance().triggerResendDesKey();
		return "Trigger resend failed DesKey success.";
	}

}